package com.controller;

import cn.edu.scau.biubiusuisui.annotation.FXController;
import cn.edu.scau.biubiusuisui.annotation.FXRedirect;
import cn.edu.scau.biubiusuisui.annotation.FXSender;
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.entity.FXRedirectParam;
import cn.edu.scau.biubiusuisui.stage.StageManager;
import com.util.PathUtil;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

import java.net.URL;

/*
 * @author: Ricky
 * @date: 2020-8-15 11:01
 */
@FXController(path = "fxml/Main.fxml")
@FXWindow(title = "测试窗口", mainStage = true, draggable = true)
public class PrimaryController extends FXBaseController {

    @FXML
    private WebView webView;

    @Override
    public void initialize() throws Exception {
        System.out.println("初始化");
        WebEngine engine = webView.getEngine();
        URL url = PathUtil.getURL("html/index.html");
        engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                JSObject win = (JSObject) engine.executeScript("window");
                win.setMember("java", this);
            }
        });

        engine.load(url.toString());
    }

    /**
     * 系统会通过发射信号，调用所有订阅这个发射信号函数的方法,从而响应信号
     *
     * @param msg
     * @return
     */
    //标注为信号发射函数
    @FXSender
    public String sendMsg(String msg) {
        return msg;
    }
}
